package com.cskaoyan.controller;


import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketStorage;
import com.cskaoyan.service.AdminStorageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 曾添乐啦
 * @since 2022/12/28 20:38
 */

@RestController
@RequestMapping("admin/storage")
public class AdminStorageController {

    @Autowired
    AdminStorageService adminStorageService;

    /**
     * 请求参数
     * http://182.92.235.201:8083/admin/storage/list?page=1&limit=20&sort=add_time&order=desc
     * page: 1
     * limit: 20
     * key:
     * name:
     * sort: add_time
     * order: desc
     * <p>
     * 响应参数
     * limit: 20
     * list[]
     * page: 1
     * pages: 25
     * total: 498
     * errmsg: "成功"
     * errno: 0
     *
     * @param basePageInfo
     * @return
     */

    @RequestMapping("list")
    public BaseRespVo AdminStorageList(BasePageInfo basePageInfo, String key, String name) {

        //      List<MarketStorage> data = adminStorageService.queryStorageList(basePageInfo);
        CommonData data = adminStorageService.queryStorageList(basePageInfo, key, name);
        return BaseRespVo.ok(data);
    }

    /**
     * 请求参数
     * http://localhost:8083/admin/storage/create
     * AdminStorageList
     * <p>
     * 响应参数
     * addTime: "2022-12-28 22:32:28"
     * id: 521
     * key: "9g0k3xjpx20w7m77m499.jpg"
     * name: "微信图片3.jpg"
     * size: 79340
     * type: "image/jpeg"
     * updateTime: "2022-12-28 22:32:28"
     * url: "http://182.92.235.201:8083/wx/storage/fetch/9g0k3xjpx20w7m77m499.jpg"
     * errmsg: "成功"
     * errno: 0
     *
     * @return
     */

    @RequestMapping("create")
    public BaseRespVo AdminStorageAdd(MultipartFile file) {

        MarketStorage marketStorage = adminStorageService.addStorage(file);


        if (marketStorage != null) {
            return BaseRespVo.ok(marketStorage);

        } else
            return BaseRespVo.invalidParameter("新增对象数据失败");
    }

    /**
     * 请求参数是JSON数据，用对象接收
     *
     * @param marketStorage
     * @return
     */

    @RequestMapping("delete")
    public BaseRespVo AdminStorageDelete(@RequestBody MarketStorage marketStorage) {
        int deleteAffected = adminStorageService.delete(marketStorage);
        if (deleteAffected == 1) {
            return BaseRespVo.ok("success");
        }
        return BaseRespVo.invalidParameter("删除对象数据失败");
    }

    @RequestMapping("update")
    public BaseRespVo AdminStorageUpdate(@RequestBody MarketStorage marketStorage) {
        int updateAffected = adminStorageService.update(marketStorage);
        if (updateAffected == 1) {
            return BaseRespVo.ok("success");
        }
        return BaseRespVo.invalidParameter("修改对象数据失败");
    }
}
